package com.lp.multi.juc.pool;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author: lp
 * @create: 2021-06-28 9:19
 * @description: 测试abort策略
 * 银行办理业务,默认开3个窗口,总共5个窗口:1,2,3,4,5
 * 当前已开窗口数3：corePoolSize
 * 当前最大窗口数=已开(3)+未开(2)=5:maximumPoolSize
 * 一个小时没有人4,5窗口关闭：keepAliveTime=1,TimeUnit=小时
 * 有3个候客厅位置：capacity
 *
 * 人太多拒绝策略：不受理，其他地方受理等
 */
public class ThreadPoolExecutorTest {
    public static void main(String[] args) {
        //最大线程定义
        //1.cpu密集型  逻辑8核cpu最多支持8线程并行，可以定义最大线程未:逻辑cpu数
        //2.IO密集型:判断程序中占用IO资源比较大的任务数N，一般设置为2N
        //Executors.newSingleThreadExecutor();
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
                3,
                5,//Runtime.getRuntime().availableProcessors()
                3,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        //线程池最大并发数=maximumPoolSize+capacity=5+3=8

        try {
            //同时运行3个，不会触发扩容到5；同时运行8个也不一定会扩容，同时运行9个会抛异常
            for (int i = 0; i < 3; i++) {
                poolExecutor.execute(()->{
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName());
                });
            }
        }finally {
            poolExecutor.shutdown();
        }

    }
}

//CallerRunsPolicy
class ThreadPoolExecutorTest2 {
    public static void main(String[] args) {
        //Executors.newSingleThreadExecutor();
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
                3,
                5,
                3,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                //A线程调用线程池，A线程处理
                new ThreadPoolExecutor.CallerRunsPolicy());
        //线程池最大并发数=maximumPoolSize+capacity=5+3=8

        try {
            //CallerRunsPolicy拒绝策略
            //最多5个线程运行，3个在等待队列。第9个线程会让main线程运行
            /**
             * 打印结果
             * pool-1-thread-2 ok
             * pool-1-thread-4 ok
             * pool-1-thread-3 ok
             * pool-1-thread-5 ok
             * main ok
             * pool-1-thread-1 ok
             * pool-1-thread-4 ok
             * pool-1-thread-2 ok
             * pool-1-thread-3 ok
             */
            for (int i = 0; i < 9; i++) {
                poolExecutor.execute(()->{
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+ " ok");
                });
            }
        }finally {
            poolExecutor.shutdown();
        }

    }
}

//DiscardPolicy
class ThreadPoolExecutorTest3 {
    public static void main(String[] args) {
        //Executors.newSingleThreadExecutor();
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
                3,
                5,
                3,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                //A线程调用线程池，A线程处理
                new ThreadPoolExecutor.DiscardPolicy());
        //线程池最大并发数=maximumPoolSize+capacity=5+3=8

        try {
            //DiscardPolicy拒绝策略
            //最多5个线程运行，3个在等待队列。第9个线程会丢弃
            /**
             * 打印结果(8个结果)
             * pool-1-thread-4 ok
             * pool-1-thread-1 ok
             * pool-1-thread-3 ok
             * pool-1-thread-2 ok
             * pool-1-thread-5 ok
             * pool-1-thread-3 ok
             * pool-1-thread-4 ok
             * pool-1-thread-1 ok
             */
            for (int i = 0; i < 9; i++) {
                poolExecutor.execute(()->{
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+ " ok");
                });
            }
        }finally {
            poolExecutor.shutdown();
        }

    }
}

//DiscardPolicy
class ThreadPoolExecutorTest4 {
    public static void main(String[] args) {
        //Executors.newSingleThreadExecutor();
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
                3,
                5,
                3,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                //A线程调用线程池，A线程处理
                new ThreadPoolExecutor.DiscardOldestPolicy());
        //线程池最大并发数=maximumPoolSize+capacity=5+3=8

        try {
            //DiscardOldestPolicy:不一定丢弃
            //最多5个线程运行，3个在等待队列。第9个线程会去尝试，看第一个有没有结束，如果结束可以运行，
            // 如果没有结束，不运行
            /**
             * 打印结果(8个结果)
             * pool-1-thread-4 ok
             * pool-1-thread-1 ok
             * pool-1-thread-3 ok
             * pool-1-thread-2 ok
             * pool-1-thread-5 ok
             * pool-1-thread-3 ok
             * pool-1-thread-4 ok
             * pool-1-thread-1 ok
             */
            for (int i = 0; i < 9; i++) {
                poolExecutor.execute(()->{
//                    try {
//                        TimeUnit.SECONDS.sleep(1);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
                    System.out.println(Thread.currentThread().getName()+ " ok");
                });
            }
        }finally {
            poolExecutor.shutdown();
        }

    }
}


